package testesOxente;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import oxente.*;

import org.junit.Assert;
import org.junit.Test;

public class TestaGerenciador {
	
	Gerenciador g;

	public TestaGerenciador() throws Exception {
		g = new Gerenciador();
	}

	@Test
	public void testCadastrarUsuario() throws Exception {
		g = new Gerenciador();

		Assert.assertTrue(g.cadastrarUsuario(new Usuario("email", "nome",
				"senha")));
		Assert.assertTrue(g.cadastrarUsuario(new Usuario("email1", "nome1",
				"senha1")));

		Assert.assertFalse(g.cadastrarUsuario(new Usuario("email", "nome",
				"senha")));
		Assert.assertFalse(g.cadastrarUsuario(new Usuario("email1", "nome1",
				"senha1")));

	}

	@Test
	public void testEliminarUsuario() throws Exception {

		Assert.assertTrue(g.cadastrarUsuario(new Usuario("email.usr3",
				"nome.usr3", "senha.usr3")));
		Assert.assertTrue(g.cadastrarUsuario(new Usuario("email.usr4",
				"nome.usr4", "senha.usr4")));

		Assert.assertTrue(g.eliminarUsuario("email.usr3"));
		Assert.assertTrue(g.eliminarUsuario("email.usr4"));
		Assert.assertFalse(g.eliminarUsuario("email.usr3"));
		Assert.assertFalse(g.eliminarUsuario("email.usr4"));

	}

	@Test
	public void testAutenticar() throws Exception {

		Assert.assertTrue(g.cadastrarUsuario(new Usuario("email.usr1",
				"nome.usr1", "senha.usr1")));
		Assert.assertTrue(g.cadastrarUsuario(new Usuario("email.usr2",
				"nome.usr2", "senha.usr2")));

		Assert.assertTrue(g.autenticar("email.usr1", "senha.usr1"));
		Assert.assertTrue(g.autenticar("email.usr2", "senha.usr2"));
		Assert.assertFalse(g.autenticar("email.usr3", "senha.usr3"));
		Assert.assertFalse(g.autenticar("email.usr4", "senha.usr4"));

		Assert.assertFalse(g.autenticar("email.usr1", "senha.usr3"));
		Assert.assertFalse(g.autenticar("email.usr2", "senha.usr4"));
		Assert.assertFalse(g.autenticar("email.usr3", "senha.usr1"));
		Assert.assertFalse(g.autenticar("email.usr4", "senha.usr2"));
	}

	@Test
	public void testEmailEhCadastrado() throws Exception {
		assertFalse(g.emailEhCadastrado("email.usr1"));		
		Assert.assertTrue(g.cadastrarUsuario(new Usuario("email.usr1",
				"nome.usr1", "senha.usr1")));
		assertTrue(g.emailEhCadastrado("email.usr1"));
	}

	@Test
	public void testAcessaUsuario() throws Exception {
		assertEquals(null, g.acessaUsuario("abcd"));
		
		Usuario usr1 = new Usuario("email.usr1",
				"nome.usr1", "senha.usr1");
		Usuario usr2 = new Usuario("email.usr2",
				"nome.usr2", "senha.usr2");
		
		
		g.cadastrarUsuario(usr1);
		g.cadastrarUsuario(usr2);
		
		assertEquals(usr1, g.acessaUsuario("email.usr1"));
		assertEquals(usr2, g.acessaUsuario("email.usr2"));
		
		assertFalse(g.acessaUsuario("email.usr1").equals(usr2));
		assertFalse(g.acessaUsuario("email.usr2").equals(usr1));
	}
}

